<?php
class GeneralReport extends ORDataObject {
	var $general_report_id	=	'';
  	var $name				=	'';
  	var $description		=	'';
  	var $group_id			=	'';
  	var $metric_name		=	'';
  	var $data_name			=	'';
  	var $data_unit			=	'';
  	var $data_type			=	'';
  	var $owner				=	'';

  	var	$_table	=	'general_report';
  	var	$_key	=	'general_report_id';

  	const	DATA_TYPE_NUM		=	'number';
  	const	DATA_TYPE_CUR		=	'currency';

  	// Default row numbers of "input data" table
  	Const 	DEFAULT_ROW_NUM		=	5;

  	public function setData($repName, $repDesc, $groupId, $metricName,
  			$dataName, $dataType, $dataUnit, $owner){
		$data	=	array(
			'name'			=>	$repName,
			'description'	=>	$repDesc,
			'group_id'		=>	$groupId,
			'metric_name'	=>	$metricName,
			'data_name'		=>	$dataName,
			'data_type'		=>	$dataType,
			'data_unit'		=>	$dataUnit,
			'owner'			=>	$owner
		);
		$this->populateArray($data);
  	}

  	public function getName(){
  		return $this->name;
  	}

  	public function getDescription(){
  		return $this->description;
  	}

  	public function getOwner(){
  		return $this->owner;
  	}

  	public function getGroupId(){
  		return $this->group_id;
  	}

  	public function getMetricName(){
  		return $this->metric_name;
  	}

  	public function getDataName(){
  		return $this->data_name;
  	}

  	public function getDataUnit(){
  		return $this->data_unit;
  	}

  	public function getDataType(){
  		return $this->data_type;
  	}

	public function getReports($userId, $groupId = null){
  		$ds		=	&new Datasource_sql();
  		$sql	=	array(
  			"cols"	=>	"$this->_key id, name, description, group_id",
  			"from"	=>	"$this->_table",
  			"where"	=>	"owner=$userId",
  			"orderby"	=>	"group_id"
  		);
  		if(!is_null($groupId)){
  			$sql['where']	.=	" AND group_id=". (int)$groupId;
  		}
  		$ds->setup($this->_db, $sql);
  		return $ds->toArray();
  	}

  	public function deleteReports($groupId = ''){
  		$sql	=	"DELETE FROM $this->_table WHERE group_id=$groupId";
  		$this->_execute($sql);
  	}

	public function moveReportsToDefaultGroup($groupId){
  		$sql	=	"UPDATE $this->_table SET group_id=0 WHERE group_id=$groupId";
  		$this->_execute($sql);
  	}

  	public function getDataTypeList(){
		$list	=	array(
			self::DATA_TYPE_NUM		=>	L10nHelper::_("Số"),
			self::DATA_TYPE_CUR		=>	L10nHelper::_("Tiền")
		);

		return $list;
  	}

  	public function deleteReport($reportId){
		$sql	=	"DELETE FROM general_report_data WHERE report_id=$reportId";
		$this->_execute($sql);

		$sql	=	"DELETE FROM $this->_table WHERE $this->_key=$reportId";
		$this->_execute($sql);
  	}
}
?>